package org.freetek.linkdn;

import org.freetek.linkdn.activity.alert.AlertActivity;
import org.freetek.linkdn.activity.alert.AlertBundleKeys;
import org.freetek.linkdn.activity.home.HomeActivity;
import org.freetek.linkdn.activity.login.LoginActivity;
import org.freetek.linkdn.activity.login.LoginBundleKeys;
import org.freetek.linkdn.util.StringUtil;
import org.freetek.linkdn.util.model.LinkedInAuthResponse;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class LinkdnActivity extends Activity {

	protected LinkedInAuthResponse getAuthResponse() {
		LinkedInAuthResponse result;
		try {
			result = (LinkedInAuthResponse) getExtras().get(LoginBundleKeys.AUTH_RESPONSE);
		} catch (ClassCastException e) {
			throw new RuntimeException("unexpected object type found at requestBundle key:"
					+ LoginBundleKeys.AUTH_RESPONSE);
		}
		if (null == result) {
			throw new RuntimeException("null found at requestBundle key:" + LoginBundleKeys.AUTH_RESPONSE);
		}
		if (StringUtil.isNullOrEmpty(result.getRequestToken())) {
			throw new RuntimeException("request_token passed through requestBundle is empty");
		}
		if (StringUtil.isNullOrEmpty(result.getRequestTokenSecret())) {
			throw new RuntimeException("request_token_secrer passed through requestBundle is empty");
		}
		return result;
	}

	protected Bundle getExtras() {
		return getIntent().getExtras();
	}

	protected Bundle createAuthBundle() {
		final Bundle result = new Bundle();
		result.putSerializable(LoginBundleKeys.AUTH_RESPONSE, getAuthResponse());

		return result;
	}

	protected Intent getAlertIntent(String msg) {
		final Bundle extras = new Bundle();
		extras.putString(AlertBundleKeys.MESSAGE, msg);
		
		final Intent result = new Intent(LinkdnActivity.this, AlertActivity.class);
		result.putExtras(extras);
		return result;
	}
}
